package com.ruoyi.statement.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.finance.collection.mapper.ChargeElementMapper;
import com.ruoyi.statement.domain.BillStatement;
import com.ruoyi.statement.service.IBillStatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author fuzhen-pc
 * @description TODO
 * @date 2023/6/25 15:37
 */
@RestController
@RequestMapping("/statement/billStatement")
public class BillStatementController extends BaseController {

    @Autowired
    private IBillStatementService billStatementService;
    @Autowired
    private ChargeElementMapper chargeElementMapper;

    /**
     * @param billCollectionIdS
     * @return
     * @description 生成对账单
     * @author fuzhen
     * @date 2023/7/6 15:14
     */
    @PostMapping("/generateBillStatement")
    @Transactional
    @Log(title = "生成对账单", businessType = BusinessType.INSERT)
    public AjaxResult generateBillStatement(@RequestBody List<Long> billCollectionIdS) {
        return  billStatementService.generateBillStatement(billCollectionIdS);
    }

    /**
     * @param billStatement
     * @return
     * @description 对账单列表查询
     * @author fuzhen-pc
     * @date 2023/7/12 11:35
     */
    @GetMapping("/selectBillStatementList")
    public TableDataInfo selectBillStatementList(BillStatement billStatement){
        startPage();
        List<BillStatement> list = billStatementService.selectBillStatementList(billStatement);
        return getDataTable(list);
    }

    /**
     * @param billStatementId
     * @return
     * @description 取消对账单
     * @author fuzhen-pc
     * @date 2023/7/12 15:05
     */
    @PostMapping("/cancelBillStatement")
    @Transactional
    @Log(title = "取消对账单", businessType = BusinessType.DELETE)
    public AjaxResult cancelBillStatement(@RequestBody Long billStatementId){
        return billStatementService.cancelBillStatement(billStatementId);
    }

    /**
     * @param
     * @return AjaxResult
     * @description 确认对账
     * @author fuzhen-pc
     * @date 2023/7/27 9:35
     */
    @PostMapping("/confirmBillStatement")
    @Transactional
    @Log(title = "对账单确认对账", businessType = BusinessType.UPDATE)
    public AjaxResult confirmBillStatement(@RequestBody Long billStatementId){
        String confirmStatus = "1";
        String collectStatus = "2";
        BillStatement billStatement = billStatementService.getById(billStatementId);
        if(confirmStatus.equals(billStatement.getStatementStatus())){
            return AjaxResult.error("该对账单已确认");
        }
        if(collectStatus.equals(billStatement.getStatementStatus())){
            return AjaxResult.error("该对账单已收款");
        }
        billStatementService.updateBillStatementStatus(billStatementId,confirmStatus);
        return AjaxResult.success("确认成功");
    }

    /**
     * @param
     * @return AjaxResult
     * @description 确认收款
     * @author fuzhen-pc
     * @date 2023/7/27 9:35
     */
    @PostMapping("/confirmBillCollection")
    @Log(title = "对账单确认收款", businessType = BusinessType.UPDATE)
    public AjaxResult confirmBillCollection(@RequestBody Long billStatementId){
        String collectStatus = "2";
        BillStatement billStatement = billStatementService.getById(billStatementId);
        if(collectStatus.equals(billStatement.getStatementStatus())){
            return AjaxResult.error("该对账单已收款");
        }
        billStatementService.updateBillStatementStatus(billStatementId,collectStatus);
        // 确认收款后从计费要素表中删除对应的计费要素
        chargeElementMapper.deleteChargeElements(billStatementId);
        return AjaxResult.success("确认成功");
    }





}
